﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Web.Api
{
    public static class ActionFactory
    {
        private static UnityContainer s_apiActionContainer;

        public static void Init(string configFilePath)
        {
            if (s_apiActionContainer == null)
            {
                s_apiActionContainer = new UnityContainer();
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = configFilePath;

                System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section = (UnityConfigurationSection)config.GetSection("WebApi");
                section.Configure(s_apiActionContainer, "Actions");
            }
        }

        public static IAction GetAction(SysParameters sysPrameters)
        {
            try
            {
                return s_apiActionContainer.Resolve<IAction>(sysPrameters.Command);
            }
            catch
            {
                return null;
            }
        }
    }
}
